<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@include file="/wx/page/session.jsp"%>
<c:set var="userId" value="${param.userId}" />
<c:set var="deptId" value="${param.deptId}" />
<c:set var="deptCode" value="${param.deptCode}" />
<c:set var="deptParentId" value="${param.deptParentId}" />
<c:set var="selfDeptState" value="${param.selfDeptState}" />
<c:set var="taskId" value="${param.taskId}" />
<c:set var="taskType" value="${param.taskType}" />
<c:set var="taskState" value="${param.taskState}" />
<c:set var="taskCostPrice" value="${param.taskCostPrice}" />
<c:set var="billId" value="${param.billId}" />
<c:set var="billState" value="${param.billState}" />
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no, width=device-width">
    <meta content="yes" name="apple-mobile-web-app-capable">
    <meta content="black" name="apple-mobile-web-app-status-bar-style">
    <title>提货单管理</title>
    <link rel="stylesheet" href="${ctx}/wx/css/default.css"/>
    <script type="text/javascript" src="${ctx}/wx/js/xtable.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/engine.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/util.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/interface/BillService.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/interface/TruckService.js"></script>
    <script type="text/javascript" src="${ctx}/wx/js/alert.js"></script>
</head>
<body>
<input id="billId" name="billId" type="hidden" value="${billId}">
<c:if test="${billState == 0 && selfDeptState == 1}">
    <p class="contact-input">
        <select id="truckPlateHeader" name="truckPlateHeader" style="width: 40px">
            <option value="蒙">蒙</option>
            <option value="晋">晋</option>
            <option value="冀">冀</option>
            <option value="辽">辽</option>
            <option value="鲁">鲁</option>
            <option value="陕">陕</option>
            <option value="豫">豫</option>
            <option value="津">津</option>
            <option value="京">京</option>
            <option value="黑">黑</option>
            <option value="吉">吉</option>
        </select>
        <select id="truckPlateMiddle" name="truckPlateMiddle" style="width: 40px">
            <option value="J">J</option>
            <option value="A">A</option>
            <option value="B">B</option>
            <option value="C">C</option>
            <option value="D">D</option>
            <option value="E">E</option>
            <option value="F">F</option>
            <option value="G">G</option>
            <option value="H">H</option>
            <option value="I">I</option>
            <option value="K">K</option>
            <option value="L">L</option>
            <option value="M">M</option>
            <option value="N">N</option>
            <option value="O">O</option>
            <option value="P">P</option>
            <option value="Q">Q</option>
            <option value="R">R</option>
            <option value="S">S</option>
            <option value="T">T</option>
        </select>
        <input type="search" id="truckPlateCode" name="truckPlateCode" minlength="5" maxlength="5" placeholder="车牌号码" style="width: 100px" onfocus="this.value=''" oninput="searchTruck(this)" required />
        <div id="truckListDiv" style="border: 1px solid #c9c9c9; width: 150px; background-color: white; visibility: hidden; position: absolute; left: 103px; top: 61px; z-index: 10;">
            <table id="truckListTable" cellpadding="5"></table>
        </div>
    </p>
    <SCRIPT LANGUAGE="JavaScript" type="">
        var billTruckThead = new Array(
                new XThead("选择", "checkbox"),
                new XThead("车牌"),
                new XThead("电话"),
                new XThead("姓名")
        );

        var billTruckXTable = new XTable("billTruck", billTruckThead, true);

        billTruckXTable.XTbodyArray = function (model) {
            var array = new Array(
                    new XTbody("billTruck", model.truckId, "checkbox", null, null, "center"),
                    new XTbody("billTruck", model.truckPlateHeader + model.truckPlateMiddle + model.truckPlateCode, null, null, null, "center"),
                    new XTbody("billTruck", model.truckMobile, null, null, null, "center"),
                    new XTbody("billTruck", model.user.userName, null, null, null, "center")
            );

            return array;
        }
        billTruckXTable.getXList = function (page){
            var truck = new Truck();
            truck.truckPlateHeader = dwr.util.getValue("truckPlateHeader");
            truck.truckPlateMiddle = dwr.util.getValue("truckPlateMiddle");
            truck.truckPlateCode = dwr.util.getValue("truckPlateCode").toUpperCase();
            TruckService.selectTruckList(truck, "${deptParentId}", function(list){billTruckXTable.addTable(list);});
        }
    </SCRIPT>
    <p class="contact-input">
        <input type="button" onclick="insertBillTruck()" value="添加车辆到提货单">
    </p>
</c:if>
<SCRIPT LANGUAGE="JavaScript" type="">
    var billThead = new Array(
            new XThead("车辆信息"),
            new XThead("司机信息"),
            new XThead("操作")
    );

    var billXTable = new XTable("bill", billThead, true);

    billXTable.XTbodyArray = function (model) {
        var href = "";
        if(model.billTruckState == 0) {
            href = "<span style='color:red'>已退款</span>";
        } else {
            <c:if test="${selfDeptState == 1}">
            if("${billState}" == 0) {
                href = "<input type='button' value='取消' onclick=\"deleteBillTruck('" + model.truckId + "')\">";
            } else if ("${billState}" == 1) {
                href = "<input type='button' value='退款' onclick='updateBillTruck(this, 0)'>";
                if("${taskType}" == 1){
                    href = "<input type='tel' value='" + model.billTruckLoad + "' style='width:40px' placeholder='吨' onfocus='this.value=\"\"' onchange='updateBillTruck(this, 1)'><br/>" + href;
                }
            }
            </c:if>
        }
        var array = new Array(
                new XTbody("bill", model.truck.truckPlateHeader + model.truck.truckPlateMiddle + model.truck.truckPlateCode + "<br>" + model.truck.truckMobile, null, null, model, "center"),
                new XTbody("bill", model.truck.user.userName + "<br>" + model.truck.user.userMobile, null, null, null, "center"),
                new XTbody("bill", href, null, null, null, "center")
        );

        return array;
    }
    billXTable.getXList = function (page){
        var billTruck = new BillTruck();
        billTruck.billId = dwr.util.getValue("billId");
        BillService.selectBillTruckList(billTruck, function(list){billXTable.addTable(list);});
    }
</SCRIPT>
<p class="contact-submit">
    <input type="button" value="返回" onclick="callback()">
</p>
</body>
</html>
<script type="text/javascript">
    if("${billId}".length > 0) {
        billXTable.goPage(1);
    }

    function searchTruck(input) {
        var truckPlateCode = input.value.toUpperCase();
        TruckService.searchTruckList("${deptParentId}", truckPlateCode, callbackSearchTruckList);
    }

    function callbackSearchTruckList(list) {
        var truckListTable = $("truckListTable");
        dwr.util.removeAllRows("truckListTable");
        $("truckListDiv").style.visibility = "visible";
        if (list.length > 0) {
            for (var i = 0; i < list.length; i++) {
                var truck = list[i];
                var row = $("truckListTable").insertRow();
                var cell = row.insertCell();
                cell.innerText = truck.truckPlateHeader + truck.truckPlateMiddle + truck.truckPlateCode;
                cell.onclick = function () {
                    dwr.util.setValue("truckPlateHeader", this.innerText.substring(0, 1));
                    dwr.util.setValue("truckPlateMiddle", this.innerText.substring(1, 2));
                    dwr.util.setValue("truckPlateCode", this.innerText.substring(2));
                    $("truckListDiv").style.visibility = "hidden";
                    getTruck();
                }
            }
        } else {
            var row = $("truckListTable").insertRow();
            var cell = row.insertCell();
            cell.innerHTML = "<a href='${ctx}/wx/page/truck_manage.jsp?userId=${userId}&deptId=${deptId}&deptCode=${deptCode}&deptParentId=${deptParentId}&truckPlateCode=" + dwr.util.getValue("truckPlateCode") + "'>添加不存在车辆</a>";
        }
    }

    function getTruck() {
        var truck = new Truck();
        truck.truckPlateHeader = dwr.util.getValue("truckPlateHeader");
        truck.truckPlateMiddle = dwr.util.getValue("truckPlateMiddle");
        truck.truckPlateCode = dwr.util.getValue("truckPlateCode").toUpperCase();
        var billTruck = new BillTruck();
        billTruck.truck = truck;
        BillService.selectBillTruckList(billTruck, function (list) {
            if(list.length > 0) {
                var billId = list[0].billId;
                var billTruck = new BillTruck();
                billTruck.billId = billId;
                BillService.selectBillTruckList(billTruck, callbackSelectBillTruckList);
            } else {
                billTruckXTable.goPage(1);
            }
        });
    }
    function callbackSelectBillTruckList(list) {
        var table = new Array(list.length);
        var billTruck;
        for (var i = 0; i < list.length; i++) {
            billTruck = list[i];
            var user = new User();
            user.userName = billTruck.truck.user.userName;
            var truck = new Truck();
            truck.truckId = billTruck.truckId;
            truck.truckPlateHeader = billTruck.truck.truckPlateHeader;
            truck.truckPlateMiddle = billTruck.truck.truckPlateMiddle;
            truck.truckPlateCode = billTruck.truck.truckPlateCode;
            truck.truckMobile = billTruck.truck.truckMobile;
            truck.user = user;
            table[i] = truck;
        }
        billTruckXTable.addTable(table);
    }
    function insertBillTruck() {
        var truckIds = billTruckXTable.getCheckBoxValue();
        if(truckIds.length > 0) {
            var billTruck = new BillTruck();
            billTruck.billId = dwr.util.getValue("billId");
            billTruck.billTruckLoad = 0;
            billTruck.billTruckState = 1;
            var bill = new Bill();
            bill.taskId = "${taskId}";
            billTruck.bill = bill;
            if(dwr.util.getValue("billId").length == 0){
                bill.billId = dwr.util.getValue("billId");
                if("${taskType}" == "1") {
                    bill.billCode = "MM";
                } else if("${taskType}" == 2) {
                    bill.billCode = "YS";
                }
                bill.billState = 0;
                bill.deptId = "${deptId}";
            }
            BillService.insertBillTruck(billTruck, truckIds, callbackInsertBillTruck);
        }
    }
    function callbackInsertBillTruck(billTruck) {
        dwr.util.setValue("billId", billTruck.billId);
        dwr.util.setValue("truckPlateCode", "");
        billXTable.goPage(1);
    }
    function updateBillTruck(obj, billTruckState) {
        var model = obj.parentElement.parentElement.model;
        var billTruck = new BillTruck();
        billTruck.billTruckState = billTruckState;
        if (billTruckState == 0) {
            if(!confirm("确认退款")){
                return false;
            }
            var task = new Task();
            task.deptId = "${deptId}";
            task.taskId = "${taskId}";
            task.taskCostPrice = "${taskCostPrice}";

            var bill = new Bill();
            bill.billId = "${billId}";
            bill.billCode = model.bill.billCode;
            bill.taskId = "${taskId}";
            bill.task = task;

            billTruck.bill = bill;
            billTruck.truck = model.truck;
        } else if(billTruckState == 1) {
            billTruck.billTruckLoad = obj.value;
        }
        billTruck.billId = model.billId;
        billTruck.truckId = model.truckId;
        BillService.updateBillTruck(billTruck, function () {
            billXTable.goPage(1);
        });
    }
    function deleteBillTruck(truckId) {
        var bill = new Bill();
        bill.billId = "${billId}";
        bill.taskId = "${taskId}";
        var billTruck = new BillTruck();
        billTruck.billId = "${billId}";
        billTruck.truckId = truckId;
        billTruck.bill = bill;
        BillService.deleteBillTruck(billTruck, function () {
            billXTable.goPage(1);
        });
    }
    function callback() {
        location = "${ctx}/wx/page/bill_list.jsp?userId=${userId}&deptId=${deptId}&deptCode=${deptCode}&deptParentId=${deptParentId}&selfDeptState=${selfDeptState}&taskId=${taskId}&taskType=${taskType}&taskState=${taskState}&taskCostPrice=${taskCostPrice}";
    }
</script>